import { proxy } from 'valtio'
import { get } from '@/request.ts'

interface searchOptions {
    // 品牌
    brand: string
    // 分区
    partition: string
    // 店铺
    shop: string
    // 来源
    source: string
    // 近日
    recently: string
    // 日期
    date: string
}

interface SelectionOptions {
    brand: { label: string; value: string }[]
    partition: { label: string; value: string }[]
    shop: { label: string; value: string }[]
    source: { label: string; value: string }[]
}

interface resultOptions {
    // 实收金额
    PaidInAmount: number
    // 实付总额
    TotalPaid: number
    // 有效订单
    validOrderNumber: number
    // 有效订单占比
    validOrderPercent: number
    // 销售订单数
    SalesOrderNumber: number
    // 人均消费金额
    PerCapitaConsumptionAmount: number
    // 新客
    NewCustomer: number
    // 老客
    patron: number
}

export const workbent = proxy({
    // 搜索对象
    searchOptions: {
        brand: '',
        partition: '',
        shop: '',
        source: '',
        recently: '',
        date: ''
    } as searchOptions,
    // 结果对象
    resultOptions: {
        PaidInAmount: 0,
        TotalPaid: 0,
        SalesOrderNumber: 0,
        validOrderNumber: 0,
        validOrderPercent: 0,
        PerCapitaConsumptionAmount: 0,
        NewCustomer: 0,
        patron: 0
    } as resultOptions,
    // 营业趋势分布图
    BusinessTrend: {
        tooltip: {},
        xAxis: {
            type: 'category',
            // boundaryGap: false,
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '金额',
                type: 'line',
                smooth: true,
                stack: '总量',
                data: []
            }
        ]
    },
    // 营收来源占比
    RevenueSources: {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            top: '5%',
            left: 'center'
        },
        series: [
            {
                name: '营收来源占比',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 40,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 66, name: '美团' },
                    { value: 33, name: '饿了么' }
                ]
            }
        ]
    },
    // 订单来源占比
    OrderSources: {
        title: {
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '订单来源占比',
                type: 'pie',
                radius: '50%',
                data: [{ value: 1048, name: 'Search Engine' }],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ],
        shop: [{ label: '所有店铺', value: '' }],
        source: [{ label: '所有来源', value: '' }]
    },
    // 获取搜索选项
    getSearch: async (filterObj = {}) => {
        const { SelectOptions, result, BusinessTrend, OrderSources, price } =
            await get('workbenth/SearchOptions', filterObj)
        workbent.brand = SelectOptions.brand
        workbent.partition = SelectOptions.partition
        workbent.shop = SelectOptions.shop
        workbent.source = SelectOptions.source
        workbent.resultOptions = result
        workbent.BusinessTrend.xAxis.data = BusinessTrend.date
        workbent.BusinessTrend.series[0].data = BusinessTrend.data
        workbent.OrderSources.series[0].data = OrderSources
        workbent.RevenueSources.series[0].data = price
    },
    // 品牌
    brand: [{ label: '所有品牌', value: '所有品牌' }],
    // 分区
    partition: [{ label: '所有分区', value: '所有分区' }],
    // 店铺
    shop: [{ label: '所有店铺', value: '所有店铺' }],
    // 来源
    source: [{ label: '所有来源', value: '所有来源' }],
    // 筛选
    filter: async (data: any) => {
        workbent.searchOptions = data
        await workbent.getSearch(workbent.searchOptions)
    }
})
